package com.zhou.scaffolding.log.aspect;

import com.zhou.scaffolding.annotation.OperationLog;
import com.zhou.scaffolding.log.event.LogEvent;
import com.zhou.scaffolding.log.event.LogEventPublish;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
@ConditionalOnClass(LogEventPublish.class)
public class OperationLogAspect {
    @Autowired
    private LogEventPublish logEventPublish;

    @Pointcut("@annotation(com.zhou.scaffolding.annotation.OperationLog)")
    public void annotationPointcut() {
    }

    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        MethodSignature methodSignature =  (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        OperationLog annotation = method.getAnnotation(OperationLog.class);
        String value = annotation.value();
       //发送spring事件

        LogEvent logEvent = new LogEvent(value);
        logEventPublish.publish(logEvent);
    }
}
